import 'package:flutter_danmu_anim/http/api.dart';
import 'package:flutter_danmu_anim/http/init.dart';
import 'package:flutter_danmu_anim/model/model_hot_video_item.dart';
import 'package:flutter_danmu_anim/model/model_owner.dart';
import 'package:flutter_danmu_anim/model/model_response.dart';
import 'package:flutter_danmu_anim/model/user/fav_detail.dart';
import 'package:flutter_danmu_anim/model/user/fav_folder.dart';
import 'package:flutter_danmu_anim/model/user/info.dart';
import 'package:flutter_danmu_anim/model/user/stat.dart';

class UserHttp {
  static Future<ResponseData<OwnerStat>> userStat({
    required int mid,
  }) =>
      tryCatch(() async {
        var res = await Request().get(Api.userStat, data: {'vmid': mid});
        if (res.data['code'] == 0) {
          var data = OwnerStat.fromJson(res.data['data']);
          return ResponseData(status: true, data: data);
        } else {
          return ResponseData(status: false);
        }
      });

  static Future<ResponseData<UserInfoData>> userInfo() =>
      tryCatch(() async {
        var res = await Request().get(Api.userInfo);
        if (res.data['code'] == 0) {
          return ResponseData(
            status: true,
            data: UserInfoData.fromJson(res.data['data']),
          );
        } else {
          return ResponseData(
            status: false,
          );
        }
      });

  static Future<ResponseData<UserStat>> userStatOwner() =>
      tryCatch(() async {
        var res = await Request().get(Api.userStatOwner);
        if (res.data['code'] == 0) {
          UserStat data = UserStat.fromJson(res.data['data']);
          return ResponseData(status: true, data: data);
        } else {
          return ResponseData(status: false);
        }
      });

  // 收藏夹
  static Future<dynamic> userfavFolder({
    required int pn,
    required int ps,
    required int mid,
  }) async {
    var res = await Request().get(Api.userFavFolder, data: {
      'pn': pn,
      'ps': ps,
      'up_mid': mid,
    });
    if (res.data['code'] == 0) {
      FavFolderData data = FavFolderData.fromJson(res.data['data']);
      return {'status': true, 'data': data};
    } else {
      return {'status': false, 'data': [], 'msg': res.data['message']};
    }
  }

  static Future<dynamic> userFavFolderDetail(
      {required int mediaId,
        required int pn,
        required int ps,
        String keyword = '',
        String order = 'mtime'}) async {
    var res = await Request().get(Api.userFavFolderDetail, data: {
      'media_id': mediaId,
      'pn': pn,
      'ps': ps,
      'keyword': keyword,
      'order': order,
      'type': 0,
      'tid': 0
    });
    if (res.data['code'] == 0) {
      FavDetailData data = FavDetailData.fromJson(res.data['data']);
      return {'status': true, 'data': data};
    } else {
      return {'status': false, 'data': [], 'msg': res.data['message']};
    }
  }

  // 稍后再看
  static Future<dynamic> seeYouLater() async {
    var res = await Request().get(Api.seeYouLater);
    if (res.data['code'] == 0) {
      List<HotVideoItemModel> list = [];
      var resList = res.data['data']['list'] ?? [];
      for (var i in resList) {
        list.add(HotVideoItemModel.fromJson(i));
      }
      return {
        'status': true,
        'data': {'list': list, 'count': res.data['data']['count']}
      };
    } else {
      return {'status': false, 'data': [], 'msg': res.data['message']};
    }
  }
}
